package cn.jbt.api.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.jbt.api.admin.annotation.Log;
import cn.jbt.api.admin.entity.XxlJobInfo;
import cn.jbt.api.admin.entity.dto.JobTriggerDTO;
import cn.jbt.api.admin.service.IXxlJobInfoService;
import cn.jbt.common.PageQryParam;
import cn.jbt.common.PageQryResult;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/xxlJob/info")
@Slf4j
@AllArgsConstructor
public class XxlJobInfoController {

    private final IXxlJobInfoService iXxlJobInfoService;

    @PostMapping("/list")
    @SaCheckPermission("xxlJob:info:list")
    @Log(bizType = "查询", bizName = "分页查询任务信息")
    public PageQryResult<XxlJobInfo> getAllByPage(@Valid @RequestBody PageQryParam<XxlJobInfo> pageQryParam) {
        return iXxlJobInfoService.getAllByPage(pageQryParam);
    }

    @PostMapping("/add")
    @SaCheckPermission("xxlJob:info:add")
    @Log(bizType = "新增", bizName = "新增任务信息")
    public XxlJobInfo add(@Valid @RequestBody XxlJobInfo xxlJobInfo) {
        return iXxlJobInfoService.add(xxlJobInfo);
    }

    @PostMapping("/edit")
    @SaCheckPermission("xxlJob:info:edit")
    @Log(bizType = "编辑", bizName = "编辑任务信息")
    public XxlJobInfo edit(@Valid @RequestBody XxlJobInfo xxlJobInfo) {
        return iXxlJobInfoService.edit(xxlJobInfo);
    }

    @PostMapping("/del")
    @SaCheckPermission("xxlJob:info:del")
    @Log(bizType = "删除", bizName = "删除任务信息")
    public void del(@NotEmpty @RequestBody List<Integer> ids) {
        iXxlJobInfoService.del(ids);
    }

    @PostMapping("/start/{id}")
    @SaCheckPermission("xxlJob:info:start")
    @Log(bizType = "启动任务", bizName = "启动任务")
    public void start(@PathVariable Integer id) {
        iXxlJobInfoService.start(id);
    }

    @PostMapping("/stop/{id}")
    @SaCheckPermission("xxlJob:info:stop")
    @Log(bizType = "停止任务", bizName = "停止任务")
    public void stop(@PathVariable Integer id) {
        iXxlJobInfoService.stop(id);
    }

    @PostMapping("/trigger")
    @SaCheckPermission("xxlJob:info:trigger")
    @Log(bizType = "执行任务", bizName = "执行任务")
    public void trigger(@Valid @RequestBody JobTriggerDTO jobTriggerDTO) {
        iXxlJobInfoService.trigger(jobTriggerDTO.getId(), jobTriggerDTO.getExecutorParam(),
                jobTriggerDTO.getAddressList());
    }


}
